﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ShareContent;


namespace AncientEmpires_025_132
{
    public class AuthenticationForm : VisibleGameEntity
    {
        #region properties
        private List<MyButton> _listButton;
        public List<MyButton> ListButton
        {
            get { return _listButton; }
            set { _listButton = value; }
        }

        private bool _isCenterPosition;
        public bool IsCenterPosition
        {
            get { return _isCenterPosition; }
            set { _isCenterPosition = value; }
        }

        private Vector2 _size;
        public Vector2 Size
        {
            get { return _size; }
            set { _size = value; }
        }

        private Vector2 _positionForm;
        public Vector2 PositionForm
        {
            get { return _positionForm; }
            set { _positionForm = value; }
        }

        private Vector2 _coordinateAccepteBtn;
        public Vector2 CoordinateAccepteBtn
        {
            get { return _coordinateAccepteBtn; }
            set { _coordinateAccepteBtn = value; }
        }

        private Vector2 _coordinateCancelBtn;
        public Vector2 CoordinateCancelBtn
        {
            get { return _coordinateCancelBtn; }
            set { _coordinateCancelBtn = value; }
        }

        private bool _isShow;
        public bool IsShow
        {
            get { return _isShow; }
            set { _isShow = value; }
        }

        private string _message;
        public string Message
        {
            get { return _message; }
            set { _message = value; }
        }
        private Vector2 _positionMessage;
        public Vector2 PositionMessage
        {
            get { return _positionMessage; }
            set { _positionMessage = value; }
        }
        #endregion

        #region Phát sinh sự kiện
        public delegate void EventHandlelect(bool isAccept);
        public event EventHandlelect SelectOnForm;
        #endregion
        
        #region Xử lý sự kiện từ button ok, cancel
        //Xử lý khi click button ok trong form
        public void ClickButtonAccept()
        {
            IsShow = false;
            SelectOnForm(true);
        }

        // Xử lý khi click button cancel trong form.
        public void ClickButtonCancel()
        {
            IsShow = false;
            SelectOnForm(false);
        }
        #endregion

        public AuthenticationForm(ContentManager content, string strtexture, List<string> strtextureacceptbtn, List<string> strtexturecancelbtn, Vector2 pos, Vector2 size, Vector2 topleft)
        {
            Texture2D[] texture = new Texture2D[1];
            texture[0] = content.Load<Texture2D>(strtexture);
            ListSprites = new List<Sprite2D>();
            Sprite2D tempSprite = new Sprite2D(texture, pos);
            tempSprite.TopLeft = topleft;
            tempSprite.Size = size;
            ListSprites.Add(tempSprite);
            Size = size;
            PositionForm = pos;
            ListButton = new List<MyButton>();
            //InitButton(content, strtextureacceptbtn, strtexturecancelbtn);
            Message = "Do you want end turn?";
            PositionMessage = pos + (new Vector2(50, 50));
        }
        public AuthenticationForm(ContentManager content, InfoAuthenticationForm info)
        {
            
            Texture2D[] texture = new Texture2D[1];
            texture[0] = content.Load<Texture2D>(info.StrXMLForm);
            ListSprites = new List<Sprite2D>();
            Sprite2D tempSprite = new Sprite2D(texture, info.PosForm);
            tempSprite.TopLeft = new Vector2();
            tempSprite.Size = info.SizeForm;
            ListSprites.Add(tempSprite);
            Size = info.SizeForm;
            PositionForm = info.PosForm;
            ListButton = new List<MyButton>();
            InitButton(content, info);
            IsCenterPosition = true;
            IsShow = false;
            Message = "Do you want end turn?";
            PositionMessage = new Vector2();
           
        }
        public void InitButton(ContentManager content, InfoAuthenticationForm info)
        {
            CoordinateAccepteBtn = info.PosAcceptButton;
            CoordinateCancelBtn = info.PosCancelButton;
            ListButton.Add(new MyButton(content, info.ListStrTextureAcceptButton, 
                                        PositionForm + CoordinateAccepteBtn,
                                        info.SizeAcceptButton, 
                                        new Vector2()
                                        )
                          );

            // Bắt sự kiện khi click button ok.
            ListButton[0].ClickButton += new MyButton.EventHandleClick(ClickButtonAccept);
            

            ListButton.Add(new MyButton(content, info.ListStrTextureCancelButton,
                                        PositionForm + CoordinateCancelBtn,
                                        info.SizeCancelButton,
                                        new Vector2()
                                        )
                          );
            // Bắt sự kiện khi click button cancel.
            ListButton[1].ClickButton += new MyButton.EventHandleClick(ClickButtonCancel);

        }

        public override void Update(GameTime gametime)
        {
            if (IsShow)
            {
                base.Update(gametime);

                for (int i = 0; i < ListButton.Count; i++)
                {
                    ListButton[i].Update(gametime);
                }
            }
            
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (IsShow)
            {
                Vector2 sizeWindow = new Vector2(spriteBatch.GraphicsDevice.Viewport.Width, spriteBatch.GraphicsDevice.Viewport.Height);
                Vector2 temp = new Vector2();
                for (int i = 0; i < ListSprites.Count; i++)
                {
                    temp.X = (sizeWindow.X - this.Size.X) / 2;
                    temp.Y = (sizeWindow.Y - this.Size.Y) / 2;
                    ListSprites[i].Position = temp;
                    ListSprites[i].TopLeft = -ListSprites[i].Position;
                }
                ListButton[0].SetPosition(ListSprites[0].Position + CoordinateAccepteBtn);
                ListButton[1].SetPosition(ListSprites[0].Position + CoordinateCancelBtn);
                base.Draw(gameTime, spriteBatch);

                // Draw string
                PositionMessage = ListSprites[0].Position + (new Vector2(this.Size.X/2, 50));
                Vector2 FontOrigin = MyFont.FontCourierNew.MeasureString(this.Message) / 2;
                spriteBatch.DrawString(MyFont.FontCourierNew, Message, PositionMessage, Color.Black,
                                        0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f);
                // Draw button
                for (int i = 0; i < ListButton.Count; i++)
                {
                    ListButton[i].Draw(gameTime, spriteBatch);
                }

            }

        }
    }
}
